function latexTableCellSummary(fid,matCell,rowNames,colNames,format,tableTitle,cornerName,caption)
% This writes a Latex table from the cell matCell into an existing document using the 
% Longtable environment 
% 
% *Inputs* 
%
% 1. fid                  File identifier 
%
% 2. matCell:             [row col] cell
%
% 3. rowNames:            [row   1] optional cell with names of the rows 
%
% 4. colNames:            [col   1] optional cell with names of the columns 
%
% 5. *format* structure 
%
%   format.rows          [1 1] or [row 1] optional cell can be either empty,'\', or '$' 
%                                 
%   format.cols          [1 1] or [col 1] optional cell can be either empty,'\', or '$'
%                         will also apply to the titles, if provided 
%
%   format.landscape      optional=1 (default if undefined is zero) 
%                         Include *\usepackage{lscape}* in the preamble 
%
%   format.font           optional string 'scriptsize','small','tiny' default is none 
% 
% 6. tableTitle           optional string to put on top of the table 
%
% 7. cornerName:           [1     1] optional string to go on the left corner of the
%                          table 
%
% 8. caption:              [1     1] optional caption at the bottom of the table 
%
%% 1. Dimensions and flags
[~, Ncol]=size(matCell);
if nargin < 3
    flags.noRowNames=1;
else
    flags.noRowNames=isempty(rowNames);
end
if nargin < 4
    flags.noColNames=1;
else
    flags.noColNames=isempty(colNames);
end
flags.landscape=0;
flags.font=0;
%% 1.i  convert row/col names to tex 

if ~flags.noRowNames
    rowNames = convert2tex(rowNames,{'PI'});
end

if ~flags.noColNames
    colNames = convert2tex(colNames);
end

%% 1.ii landcape and font
if nargin > 4
    if isempty(format)==false
        if isfield(format,'landscape')==true && isempty(format.landscape)==false
            if format.landscape==1
                flags.landscape=1;
            end
        end
        if isfield(format,'font')==true && isempty(format.font)==false
            flags.font=1;
        end
    end
end

if flags.landscape
    if Ncol < 9
        latexTableCell(fid,matCell,rowNames,colNames,format,tableTitle,cornerName,caption)
    else
        for i = 1:floor(Ncol/8)
            latexTableCell(fid,matCell(:,8*i-7:8*i),rowNames,colNames(8*i-7:8*i),format,tableTitle,cornerName,caption)
        end
        if 8*i ~= Ncol
            latexTableCell(fid,matCell(:,8*i+1:Ncol),rowNames,colNames(8*i+1:Ncol),format,tableTitle,cornerName,caption)
        end
    end
else
    if Ncol < 5
        latexTableCell(fid,matCell,rowNames,colNames,format,tableTitle,cornerName,caption)
    else
        for i = 1:floor(Ncol/4)
            latexTableCell(fid,matCell(:,4*i-3:4*i),rowNames,colNames(4*i-3:4*i),format,tableTitle,cornerName,caption)
        end
        if 4*i ~= Ncol
            latexTableCell(fid,matCell(:,4*i+1:Ncol),rowNames,colNames(4*i+1:Ncol),format,tableTitle,cornerName,caption)
        end
    end
end

end
